package com.keyware.kd.service.impl.message;

import java.util.Date;
import java.util.List;

import com.keyware.utils.ProjectIdentNew;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.keyware.kd.repository.entity.index.MessageTable;
import com.keyware.kd.repository.mybatis.itf.index.MessageTableMapper;
import com.keyware.kd.service.itf.message.MessageService;
import com.keyware.kd.service.itf.number.NumberBuilderService;
import com.keyware.kd.service.itf.todo.CallTodoService;
import com.keyware.kd.utils.Constant;
import com.keyware.kd.vo.index.MessageTableVo;
import com.keyware.utils.DateUtils;
import com.keyware.utils.IdGenerator;
import javax.servlet.http.HttpServletRequest;

@Service
public class MessageServiceImpl implements MessageService{

	@Autowired
	private MessageTableMapper messageTableMapper;

	@Autowired
	private NumberBuilderService numberBuilderService;

	@Autowired
	private CallTodoService callTodoService;

	private  HttpServletRequest request;

	public HttpServletRequest getRequest() {
		return request;
	}

	@Autowired
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	
	@Override
	public void insertMessage(String messageContent, String modelName,
			String messageUrl, String receiverId, String projectId,
			String status,String tableFlag) {
		String[] receiverIds = receiverId.split(",");
		for(int i=0;i<receiverIds.length;i++){
			MessageTable messageTable = new MessageTable();
			messageTable.setMessageId(IdGenerator.uuid32());
			messageTable.setParentModelName(modelName);
			messageTable.setMessageUrl(messageUrl);
			messageTable.setReceiverId(receiverIds[i]);
			messageTable.setCreateTime(DateUtils.formatDateTime(new Date()));
			messageTable.setMessageStatus("0");
			messageTable.setProjectId(projectId);
			messageTable.setMessageContent(messageContent);
			messageTable.setReadHande(status);
			messageTable.setProjectSource(Constant.projectName);
			messageTable.setTableFlag(tableFlag);
			messageTableMapper.insertSelective(messageTable);
			interfaceFun(messageTable,"create");
		}
	}
	
	@Override
	public List<MessageTable> selectByInfo(MessageTableVo record) {
		record.setProjectSource(Constant.projectName);
		return messageTableMapper.selectByInfo(record);
	}

	@Override
	public int updateByPrimaryKeySelective(MessageTable record) {
		 int index = messageTableMapper.updateByPrimaryKeySelective(record);
		 interfaceFun(messageTableMapper.selectByPrimaryKey(record.getMessageId()),"update");
		 return index;
	}

	@Override
	public void updateMessageStatus(String id) {
		MessageTable messageTable = new MessageTable();
		messageTable.setMessageId(id);
		messageTable.setHandeTime(DateUtils.formatDateTime(new Date()));
		messageTable.setMessageStatus("1");
		this.updateByPrimaryKeySelective(messageTable);
		interfaceFun(messageTableMapper.selectByPrimaryKey(id),"update");
	}

	@Override
	public int deleteByProjectId(String projectId) {
		 int index = messageTableMapper.deleteByProjectId(projectId,Constant.projectName);
			String numCode = (String) request.getSession().getAttribute("customCode");
			if(numCode != null && numCode.equals(ProjectIdentNew.siyuan17suo)){
				MessageTableVo record = new MessageTableVo();
				record.setProjectId(projectId);
				record.setProjectSource(Constant.projectName);
				List<MessageTable> messageTables=selectByInfo(record);
				for (MessageTable mt : messageTables) {
					if("1".equals(mt.getReadHande())){
						interfaceFun(mt,"delete");
					}
				}
			}
			return index;
	}

	@Override
	public MessageTable selectByPrimaryKey(String messageId) {
		
		return messageTableMapper.selectByPrimaryKey(messageId);
	}

	@Override
	public void insertMessageByObj(MessageTable messageTable) {
		messageTableMapper.insertSelective(messageTable);
	}

	public void interfaceFun(MessageTable messageTable,String flag){
		String numCode = numberBuilderService.getCustomerId();
		if(numCode != null && numCode.equals(ProjectIdentNew.siyuan17suo)){
			try {
				if("1".equals(messageTable.getReadHande())){
					if("create".equals(flag)){
						callTodoService.callCreateOfficeService(messageTable);						
					}else if("update".equals(flag)){
						callTodoService.callSetOfficeOverService(messageTable.getMessageId());
					}else if("delete".equals(flag)){
						callTodoService.callDeleteOfficeService(messageTable.getMessageId());
					}
				}
			} catch (Exception e) {
				MessageTable messageTable1 = new MessageTable();
				messageTable1.setMessageId(IdGenerator.uuid32());
				messageTable1.setParentModelName(messageTable.getParentModelName());
				messageTable1.setMessageUrl("");
				messageTable1.setReceiverId(messageTable.getReceiverId());
				messageTable1.setCreateTime(DateUtils.formatDateTime(new Date()));
				messageTable1.setMessageStatus("0");
				messageTable1.setProjectId(messageTable.getProjectId());
				messageTable1.setMessageContent("无法调用OA软件接口，请检查");
				messageTable1.setReadHande("0");
				messageTable1.setProjectSource(Constant.projectName);
				messageTable1.setTableFlag(messageTable.getTableFlag());
				messageTableMapper.insertSelective(messageTable1);
			}
		   }
	 }
}
